<template>
  <div>
    <!-- 面包屑导航区域 -->
    <el-breadcrumb separator-class="el-icon-arrow-right">
      <el-breadcrumb-item :to="{ path: '/welcome' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item>商品管理</el-breadcrumb-item>
      <el-breadcrumb-item>商品列表</el-breadcrumb-item>
    </el-breadcrumb>

    <!-- 卡片区域 -->
    <el-card>
      <el-row :gutter="20">
        <el-col :span="8">
          <el-input
            placeholder="请输入内容"
            v-model="queryInfo.query"
            clearable
            @clear="getGoodsList()"
          >
            <el-button
              slot="append"
              icon="el-icon-search"
              @click="getGoodsList()"
            ></el-button>
          </el-input>
        </el-col>
        <el-col :span="4">
          <el-button type="primary" @click="addAge()">添加商品</el-button>
        </el-col>
      </el-row>

      <!-- Table区域 -->
      <el-table :data="goodsList" style="width: 100%" border stripe>
        <el-table-column type="index" label="#"></el-table-column>
        <el-table-column prop="goods_name" label="商品名称"> </el-table-column>
        <el-table-column prop="goods_price" label="商品价格(元)" width="95">
        </el-table-column>
        <el-table-column prop="goods_weight" label="商品重量" width="95">
        </el-table-column>
        <el-table-column prop="add_time" label="创建时间" width="150">
          <template slot-scope="scope">
            {{ scope.row.add_time | dataFormat }}
          </template>
        </el-table-column>
        <el-table-column label="操作" width="200">
          <template slot-scope="scope">
            <el-button
              type="primary"
              class="el-icon-edit"
              @click="editGood(scope.row.goods_id)"
            ></el-button>
            <el-button
              type="danger"
              class="el-icon-delete"
              @click="deleteGood(scope.row.goods_id)"
            ></el-button>
          </template>
        </el-table-column>
      </el-table>

      <!-- 分页区域 -->
      <el-pagination
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="queryInfo.pagenum"
        :page-sizes="[5, 10, 15, 20]"
        :page-size="queryInfo.pagesize"
        layout="total, sizes, prev, pager, next, jumper"
        :total="total"
        background
      >
      </el-pagination>
    </el-card>
    <!-- 点击编辑  弹出对话框 -->
    <el-dialog
      title="编辑商品"
      :visible.sync="eidtGoodDialogVisible"
      width="50%"
    >
      <el-form ref="editGoodformRef"  :model="editGoodform" label-width="80px">
        <el-form-item label="商品名称">
          <el-input v-model="editGoodform.goods_name"></el-input>
        </el-form-item>
        <el-form-item label="商品价格">
          <el-input v-model="editGoodform.goods_price"></el-input>
        </el-form-item>
        <el-form-item label="商品重量">
          <el-input v-model="editGoodform.goods_weight"></el-input>
        </el-form-item>
        <el-form-item label="商品数量">
          <el-input v-model="editGoodform.goods_number"></el-input>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="eidtGoodDialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="eidtGoodDialog()">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
export default {
  data() {
    return {
      // 查询参数
      queryInfo: {
        query: "",
        pagenum: 1,
        pagesize: 5,
      },
      // 商品列表
      goodsList: [],
      // 总计数
      total: 0,
      editGoodform: {},
      eidtGoodDialogVisible: false,

    };
  },
  created() {
    // 调用获取商品列表数据
    this.getGoodsList();
  },
  methods: {
    // 调用获取商品列表数据
    async getGoodsList() {
      const { data: res } = await this.$http.get("goods", {
        params: this.queryInfo,
      });
      console.log(res);
      if (res.meta.status !== 200) {
        return this.$Message.error("获取商品列表数据失败！");
      }
      this.goodsList = res.data.goods;
      this.total = res.data.total;
      this.$Message.success("获取商品列表数据成功！");
    },
    handleSizeChange(newSize) {
      this.queryInfo.pagesize = newSize;
      this.getGoodsList();
    },
    handleCurrentChange(newPage) {
      this.queryInfo.pagenum = newPage;
      this.getGoodsList();
    },
    // 监听删除事件
    async deleteGood(id) {
      const delConfig = await this.$confirm(
        "此操作将永久删除该商品, 是否继续?",
        "提示",
        {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning",
        }
      ).catch((err) => err);
      // console.log(delConfig);
      if (delConfig !== "confirm")
        return this.$Message.info("您取消了删除操作！");
      const { data: res } = await this.$http.delete("goods/" + id);
      console.log(res);
      if (res.meta.status !== 200) {
        return this.$Message.error("删除分类失败");
      }
      this.$Message.success("删除分类成功");
      // 先刷新数据
      this.getGoodsList();
      // this.eidtCateDialogVisible = false;
    },
    // 监听编辑按钮
    async editGood(id) {
      const { data: res } = await this.$http.get("goods/" + id);
      // console.log(res);
      if (res.meta.status !== 200) {
        return this.$Message.error("查询失败");
      }
      this.$Message.success("查询商品成功！");
      this.editGoodform = res.data;
      this.eidtGoodDialogVisible = true;
    },
        //  监听编辑按钮 点击确定事件
    eidtGoodDialog() {
      this.$refs.editGoodformRef.validate(async (valid) => {
        if (!valid) return;
        const { data: res } = await this.$http.put(
          "goods/" + this.editGoodform.goods_id,
          { goods_name: this.editGoodform.goods_name,
          goods_price: this.editGoodform.goods_price ,
          goods_number: this.editGoodform.goods_number ,
          goods_name: this.editGoodform.goods_weight  }
        );
        console.log(res);
        if (res.meta.status !== 200) {
          return this.$Message.error(res.meta.msg);
        }
        this.$Message.success("编辑提交信息成功");
        // 先刷新数据
        this.getGoodsList();
        this.eidtGoodDialogVisible = false;
      });
    },
    addAge(){
      this.$router.push('/goods/add')
    }
  },
};
</script>

<style scoped>
.el-table {
  margin-top: 20px;
}
</style>